import execjs
import requests
import time
import base64
import json


# 3: 数字和英文混合
def base64_api(img_base64, typeid):
    data = {"username": "yuan0316", "password": "yuan0316", "typeid": typeid, "image": img_base64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        # ！！！！！！！注意：返回 人工不足等 错误情况 请加逻辑处理防止脚本卡死 继续重新 识别
        return result["message"]
    return ""


# (1) 加载首页cookie

session = requests.session()
session.headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
}

session.get("https://www.epwk.com/login.html")

# (2) 加载js代码
f = open("yipinweike.js", mode='r', encoding="utf-8")
js_code = f.read()
f.close()
js = execjs.compile(js_code)

# 获取验证码
data01 = {

}
headers01 = js.call("fn", data01)
session.headers = headers01

url = "https://www.epwk.com/api/epwk/v1/captcha/show?channel=common_channel&base64=1"
resp = session.get(url)
img_b64 = resp.json().get("data").get("base64")
code = base64_api(img_b64, 3)
print(code)

data = {
    "username": "yuan",
    "password": "yuan",
    "code": code,
    "hdn_refer": "https://www.epwk.com/"
}

headers = js.call("fn", data)
session.headers = headers

url = "https://www.epwk.com/api/epwk/v1/user/login"
resp = session.post(url, data=data)
print(resp.text)
